package com.yuexin.project.store.sort.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yuexin.common.utils.security.ShiroUtils;
import com.yuexin.framework.aspectj.lang.annotation.Log;
import com.yuexin.framework.aspectj.lang.enums.BusinessType;
import com.yuexin.framework.web.controller.BaseController;
import com.yuexin.framework.web.domain.AjaxResult;
import com.yuexin.framework.web.page.TableDataInfo;
import com.yuexin.project.store.sort.domain.GoodsSort;
import com.yuexin.project.store.sort.mapper.GoodsSortMapper;
import com.yuexin.project.store.sort.service.IGoodsSortService;

/**
 * 物品分类 信息操作处理
 * 
 * @author yuexin
 * @date 2018-11-29
 */
@Controller
@RequestMapping("/store/goodsSort")
public class GoodsSortController extends BaseController
{
    private String prefix = "store/goodsSort";
	
	@Autowired
	private IGoodsSortService goodsSortService;
	
	@Autowired
	private GoodsSortMapper goodsSortMapper;
	
	@RequiresPermissions("store:goodsSort:view")
	@GetMapping()
	public String goodsSort()
	{
	    return prefix + "/goodsSort";
	}
	
	/**
	 * 查询物品分类列表
	 */
	@RequiresPermissions("store:goodsSort:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(GoodsSort goodsSort)
	{
		startPage();
		goodsSort.setCompanyId(ShiroUtils.getCompanyId());
        List<GoodsSort> list = goodsSortService.selectGoodsSortList(goodsSort);
		return getDataTable(list);
	}
	
	/**
	 * 新增物品分类
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	 /**
	  * 校验名称
     */
    @PostMapping("/checkNameUnique")
    @ResponseBody
    public String checkNameUnique(GoodsSort goodsSort)
    {
    	int names = goodsSortMapper.checkName(goodsSort.getSortName());
		if (names > 0)
        {
			return "1";
        }
        return "0";
    }
	
	/**
	 * 新增保存物品分类
	 */
	@RequiresPermissions("store:goodsSort:add")
	@Log(title = "物品分类", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(GoodsSort goodsSort)
	{		
		goodsSort.setCompanyId(ShiroUtils.getCompanyId());
		return toAjax(goodsSortService.insertGoodsSort(goodsSort));
	}

	/**
	 * 修改物品分类
	 */
	@GetMapping("/edit/{sortId}")
	public String edit(@PathVariable("sortId") Integer sortId, ModelMap mmap)
	{
		GoodsSort goodsSort = goodsSortService.selectGoodsSortById(sortId);
		mmap.put("goodsSort", goodsSort);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存物品分类
	 */
	@RequiresPermissions("store:goodsSort:edit")
	@Log(title = "物品分类", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(GoodsSort goodsSort)
	{		
		return toAjax(goodsSortService.updateGoodsSort(goodsSort));
	}
	
	/**
	 * 删除物品分类
	 */
	@RequiresPermissions("store:goodsSort:remove")
	@Log(title = "物品分类", businessType = BusinessType.DELETE)
	@PostMapping( "/remove/{sortId}")
	@ResponseBody
	public AjaxResult remove(@PathVariable("sortId") Integer sortId)
	{		
		int result = goodsSortMapper.checkSortExistGoods(sortId);
        if(result > 0) {
        	return error(1, "分类存在物品，不允许删除");
        }
		return toAjax(goodsSortService.deleteGoodsSortById(sortId));
	}
	
}
